﻿using Newtonsoft.Json;
using Org.BouncyCastle.Ocsp;
using RestSharp;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using XXX.api.Log;
using XXX.Bo;
using XXX.Common;
using XXX.Models;

namespace ZDXieTong.Bo
{
    public class WebPostService
    {
        /// <summary>
        /// 从SDC读取
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="cjson">组织代码</param>  
        public static string GetSDC(string url, string cjson, string acctoken)
        {
            try
            {
                //记录日志
                LoggerHelper logger = new LoggerHelper();
                logger.Debug("", "读取SDC:" + url);
                logger.Debug("", "读取SDC:" + cjson);
                //推送

                var client = new RestClient(url);
                client.Timeout = -1;
                var request = new RestRequest(Method.GET);
                request.AddHeader("Content-Type", "multipart/form-data");
                //token 不为空的，添加token
                if (!string.IsNullOrWhiteSpace(acctoken))
                {
                    request.AddHeader("Authorization", "Bearer " + acctoken);
                }
                request.AddParameter("multipart/form-data", cjson, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                logger.Debug("", "读取SDC结果:" + response.Content);

                if (response.IsSuccessful)
                {
                    return response.Content;

                }
                else
                {

                    return response.StatusDescription;
                }


            }
            catch (Exception ex)
            {

                return ex.Message;
            }
        }



        /// <summary>
        /// 推送给MES
        /// </summary>
        /// <param name="docType">业务类型</param>
        /// <param name="cOrg">组织代码</param>
        /// <param name="iid">推送的中间表id</param>
        /// <param name="cCode">单据号</param>
        /// <param name="data">构造的datalist</param>
        /// <returns>code=-1，代表接口出错，-2代表程序出库，0000代表成功</returns>
        public static string PostWMS(string url, string cjson,string acctoken)
        {
            try
            {
                //记录日志
                LoggerHelper logger = new LoggerHelper();
                logger.Debug("","推送SDC:"+ url);
                logger.Debug("", "推送SDC:" + cjson);
                //推送
                var client = new RestClient(url);
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                //token 不为空的，添加token
                if (!string.IsNullOrWhiteSpace(acctoken))
                {
                    request.AddHeader("Authorization", "Bearer "+acctoken);
                }
                request.AddParameter("application/json", cjson, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);

                logger.Debug("", "推送SDC结果:" + response.Content);
                //写入U8 接口日志
               
                if (response.IsSuccessful)
                {
                    return response.Content ;
                    
                }
                else
                {

                    return response.StatusDescription;
                }

                 
            }
            catch (Exception ex)
            {
                
                return ex.Message;
            }
        }
    }
}
